package cn.tedu.sp02.controller;

import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.service.ItemService;
import cn.tedu.sp01.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@RestController
@Slf4j
public class ItemController {
    @Autowired
    private ItemService itemService;

    //获取订单商品列表
    @GetMapping("/{orderId}")
    public JsonResult<List<Item>> getItems(@PathVariable String orderId) throws InterruptedException {
        List<Item> items = itemService.getItems(orderId);

        /**
         * 延迟测试ribbon的超时重试
         */
        //随机延迟代码
        if (Math.random()<0.6){//60%概率执行延迟
            //随机延迟时长[0,5000)
            int t = new Random().nextInt(5000);
            log.info("暂停: "+t);
            Thread.sleep(t);
        }

        return JsonResult.ok().data(items);
    }

    /**
     * @RequestBody 完整的接受请求协议体数据,在转换为List<Item>
     * @param items
     * @return
     */
    //减少商品库存,使用postman测试post请求
    @PostMapping("/decreaseNumber")
    public JsonResult<?> decreaseNumber(@RequestBody List<Item> items){
        itemService.decreaseNumber(items);
        return JsonResult.ok().msg("减少商品库存成功");
    }

    //图标请求
    @GetMapping("/favicon.ico")
    public void ioc() {
    }
}
